<?php

// $Id$

require_once('te_tree_menu_item_already_exists.class.php');
require_once('tree_menu_item.class.php');


Class Tree_Menu extends Tangra_Class {
	protected $items = array();

	public function set_items($items) {
		if (is_array($items)) {
			foreach($items as $item) {
				if ($item instanceof Tree_Menu_Item) {
					if (!array_key_exists($item->get_hid(), $this->items)) {
						$this->items[$item->get_hid()] = $item;
					} else {
						throw new TE_Tree_Menu_Item_Already_Exists($item->get_hid());
					}
				} else {
					throw new TE_Tree_Menu('$items contains item that is not Tree_Menu_Item');
				}
			}
		} else {
			throw new TE_Tree_Menu('$items is not an array.');
		}
	}


	public function add_item(Tree_Menu_Item $item) {
		if (!$this->item_exists($item)) {
			$this->items[$item->get_hid()] = $item;
		} else {
			throw new TE_Tree_Menu_Item_Already_Exists($item->get_hid());
		}
	}


	public function get_items() {
		return $this->items;
	}


	public function item_exists(Tree_Menu_Item $item) {
		return array_key_exists($item->get_hid(), $this->items);
	}
}